/**
 *var $username   用户名
  var $password   mima
  var $userid     id
  var user        all
 */

var imgurl;

$(document).ready(function () {
    init()

    $('#image').cropper({
        aspectRatio:1/1,
        dragMode:"move",
        preview:$('#view'),
        crop : function () {
        },
    });
})

function init()
{
    $('#username').text($username);
    $('#phone').val(user.phone);
    $("#Addr").val(user.addr);
    $("#email").val(user.email);
}

$("#reset").on('click',function () {
   Update()
})

$('.close').on('click' ,function () {
    $("#myAlert").addClass('hide');
})

$("#inputfile").on('change',function () {
    files = $(this).prop('files');

    if (files.length > 0) {
        file = files[0];
    }
        if (imgurl) {
            URL.revokeObjectURL(this.url); // Revoke the old one
        }
        imgurl = URL.createObjectURL(file);
    $('#image').cropper('replace',imgurl);

})


function Update() {

    var imageurl=$('#image').cropper('getCroppedCanvas',{
        width:200,
        height:200
    }).toDataURL("image/jpg").split(',')[1];
    console.log(imageurl.length)
    var json={username:$username,picturestr:imageurl}
    $.ajax({
        url:url+"person/uploadpicture.do",
        data:JSON.stringify(json),
        type:"post",
        contentType :'application/json;charset=UTF-8',
        success:function (data) {
            console.log(data);
           user.imagesrc=data;
            $.ajax({
                url:url+'person/updateInfo.do',
                type:'get',
                datatype:'json',
                data:user,
                success:function (data) {
                    if(data=="success"){
                        $("#myAlert").removeClass('hide');
                        $("#userpicture").attr("src",user.imagesrc+"?"+new Date());//+?才能重新加载图片否则会调用缓存的图片
                    }
                }
            })
        }
    })
}


//





